package com.iplant.basic.controller.crm;
 
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.iplant.basic.service.po.crm.CRMCustomerType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.CRMService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.crm.CRMCustomer;

@RestController
@RequestMapping("/api/Customer")
public class CustomerController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(CustomerController.class);

	@Autowired
	CRMService wCRMService;

	@GetMapping("/All")
	public Object All(HttpServletRequest request) {

		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			String wTaxCode = StringUtils.parseString(request.getParameter("TaxCode"));
			String wCustomerName = StringUtils.parseString(request.getParameter("CustomerName"));
			String wIndustry = StringUtils.parseString(request.getParameter("Industry"));
			String wCountry = StringUtils.parseString(request.getParameter("Country"));
			String wProvince = StringUtils.parseString(request.getParameter("Province"));
			String wCity = StringUtils.parseString(request.getParameter("City"));
			int wGrade = StringUtils.parseInt(request.getParameter("Grade"));
			int wType = StringUtils.parseInt(request.getParameter("Type"));
			int wActive = StringUtils.parseInt(request.getParameter("Active"));
			Pagination wPagination = GetPagination(request);

			ServiceResult<List<CRMCustomer>> wServiceResult = wCRMService.CRM_QueryCustomerList(wLoginUser, wTaxCode,
					wCustomerName, wIndustry, wCountry, wProvince, wCity, wGrade, wType, wActive, wPagination);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(),
						wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/Info")
	public Object Info(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);
			int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
			String wCustomerCode = StringUtils.parseString(request.getParameter("CustomerCode"));

			ServiceResult<CRMCustomer> wServiceResult = wCRMService.CRM_QueryCustomer(wBMSEmployee, wCustomerID,
					wCustomerCode);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}

		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/Active")
	public Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			List<CRMCustomer> wCRMCustomerList = CloneTool.CloneArray(wParam.get("data"), CRMCustomer.class);

			int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

			if (wCRMCustomerList == null || wCRMCustomerList.size() < 1) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_IN);
				return wResult;
			}

			ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);

			for (CRMCustomer wCRMCustomer : wCRMCustomerList) {
				if (wCRMCustomer.ID <= 0)
					continue;

				wServiceResult = wCRMService.CRM_ActiveCustomer(wBMSEmployee, wCRMCustomer, wActive);

				if (StringUtils.isNotEmpty(wServiceResult.getFaultCode()))
					break;
			}

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/Update")
	public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			CRMCustomer wCRMCustomer = CloneTool.Clone(wParam.get("data"), CRMCustomer.class);

			ServiceResult<Integer> wServiceResult = wCRMService.CRM_UpdateCustomer(wBMSEmployee, wCRMCustomer);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wCRMCustomer);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wCRMCustomer);
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/Delete")
	public Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			CRMCustomer wCRMCustomer = CloneTool.Clone(wParam.get("data"), CRMCustomer.class);

			ServiceResult<Integer> wServiceResult = wCRMService.CRM_DeleteCustomer(wBMSEmployee, wCRMCustomer);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wCRMCustomer);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wCRMCustomer);
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}



	@GetMapping("/TypeAll")
	Object TypeAll(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);
			String wName = StringUtils.parseString(request.getParameter("Name"));
			int wActive = StringUtils.parseInt(request.getParameter("Active"));
			Pagination wPagination = GetPagination(request);
			ServiceResult<List<CRMCustomerType>> wServiceResult = wCRMService.CRM_QueryCustomerTypeList(wLoginUser, wName, wActive, wPagination);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/TypeInfo")
	Object TypeInfo(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			int wID = StringUtils.parseInt(request.getParameter("ID"));
			String wCode = StringUtils.parseString(request.getParameter("Code"));

			ServiceResult<CRMCustomerType> wServiceResult = wCRMService.CRM_QueryCustomerType(wLoginUser, wID, wCode);
			CRMCustomerType wServerRst = wServiceResult.Result;

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
			}

		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/TypeUpdate")
	Object TypeUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);
			int wUserID = wBMSEmployee.ID;
			String wUserName = wBMSEmployee.Name;

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			CRMCustomerType wCRMCustomerType = CloneTool.Clone(wParam.get("data"), CRMCustomerType.class);
			wCRMCustomerType.EditorName = wUserName;
			wCRMCustomerType.EditorID = wUserID;
			wCRMCustomerType.EditTime = Calendar.getInstance();
			if (wCRMCustomerType.ID <= 0) {

				wCRMCustomerType.CreatorName = wUserName;
				wCRMCustomerType.CreatorID = wUserID;
				wCRMCustomerType.CreateTime = Calendar.getInstance();

			}
			ServiceResult<Integer> wServiceResult = wCRMService.CRM_UpdateCustomerType(wBMSEmployee, wCRMCustomerType);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wCRMCustomerType);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/TypeDelete")
	Object TypeDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			CRMCustomerType wCRMCustomerType = CloneTool.Clone(wParam.get("data"), CRMCustomerType.class);

			ServiceResult<Integer> wServiceResult = wCRMService.CRM_DeleteCustomerType(wBMSEmployee, wCRMCustomerType);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/TypeActive")
	Object TypeActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

			List<CRMCustomerType> wCRMCustomerTypeList = CloneTool.CloneArray(wParam.get("data"), CRMCustomerType.class);
			ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
			for (CRMCustomerType wCRMCustomerType : wCRMCustomerTypeList) {

				wServiceResult = wCRMService.CRM_ActiveCustomerType(wBMSEmployee, wCRMCustomerType, wActive);

				if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
					break;
			}

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}




}